using Janus.Cores.IOC;
using Janus.Cores.RealProxy;
using Microsoft.Practices.Unity;
using System;
using System.Linq;
using System.Runtime.Remoting.Proxies;

public class ServiceFactory
{
    public static System.Reflection.Assembly LoadSQLServerDAL()
    {
        return System.Cores.IOC.ServiceFactory.LoadService("*.SQLServerDAL.dll");
    }
    public static System.Reflection.Assembly LoadOracleDAL()
    {
        return System.Cores.IOC.ServiceFactory.LoadService("*.OracleDAL.dll");
    }

    public static System.Reflection.Assembly LoadServices()
    {
        return System.Cores.IOC.ServiceFactory.LoadService("*.Services.dll");
    }

    public static void Register(System.Reflection.Assembly service)
    {
        //System.Cores.IOC.ServiceFactory.Register(service);
        System.Type[] types = service.GetTypes();
        System.Type[] array = types;
        for (int i = 0; i < array.Length; i++)
        {
            System.Type typeClass = array[i];
            System.Type[] interfaces = typeClass.GetInterfaces();
            //if (typeClass.Name.IndexOf("BaseDal")<0)
            {
                interfaces = interfaces.Where(c => c.Name.IndexOf("IBase") < 0).ToArray();
            }
            bool flag = interfaces.Length > 1;
            for (int j = 0; j < interfaces.Length; j++)
            {
                Type typeInterface = interfaces[j];
                IdalProxyAttribute imp = new IdalProxyAttribute();
                var instance = imp.GetServices(typeClass);
                if (flag)
                {
                    Container.Current.RegisterType(typeInterface, typeClass, typeInterface.Name, new InjectionMember[0]);
                }
                else
                {
                    Container.Current.RegisterType(typeInterface, typeClass, new InjectionMember[0]);
                }
            }
        }
    }
    public static void RegisterFromProxy(System.Reflection.Assembly service)
    {
        System.Type[] types = service.GetTypes();
        System.Type[] array = types;
        for (int i = 0; i < array.Length; i++)
        {
            System.Type typeClass = array[i];
            System.Type[] interfaces = typeClass.GetInterfaces();
            //if (typeClass.Name.IndexOf("BaseDal")<0)
            {
                interfaces = interfaces.Where(c => c.Name.IndexOf("IBase") < 0).ToArray();
            }
            bool flag = interfaces.Length > 1;
            for (int j = 0; j < interfaces.Length; j++)
            {
                Type typeInterface = interfaces[j];
                IdalProxyAttribute imp = new IdalProxyAttribute();
                var instance = imp.GetServices(typeClass);
                if (flag)
                {
                    Container.Current.RegisterInstance(typeInterface, typeInterface.Name, instance);
                    //Container.Current.RegisterType(typeInterface, typeClass, typeInterface.Name, new InjectionMember[0]);
                }
                else
                {
                    Container.Current.RegisterInstance(typeInterface, instance);
                    //Container.Current.RegisterType(typeInterface, typeClass, new InjectionMember[0]);
                }
            }
        }
    }

    public static T GetService<T>()// where T : class
    {
        return Container.Current.Resolve<T>(new ResolverOverride[0]);
    }
    /// <summary>
    /// Create object by specil name.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="name"></param>
    /// <returns></returns>
    public static T GetService<T>(string name) //where T : class
    {
        return Container.Current.Resolve<T>(name, new ResolverOverride[0]); //System.Cores.IOC.ServiceFactory.GetService<T>(name);
    }
    /// <summary>
    /// Create object use lazy.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static T LazyT<T>() //where T : class
    {
        //return System.Cores.IOC.ServiceFactory.LazyT<T>();
        Lazy<T> lazy = new Lazy<T>(() => ServiceFactory.GetService<T>());
        return lazy.Value;
    }

}
